<template>

</template>

<script>
  import { $POST, $GET } from '@/utils/api.js' // 接口api
  export default{
    data() {
      return{ 
         codeurl:'', // 需要传给后台的code
      }
    },
     onLoad(params) {
       this.codeurl = this.getUrlCode().code//获取code
       this.getAuthorization() // 授权
    },
    mounted() {
      // this.codeurl = this.getUrlCode().code //获取code
      // this.getAuthorization() // 授权
    },
     methods:{
       // 获取个人信息
       async getUsser() {
         const res = await $GET('/account/my-profile')
         if (res.userName) {
           // 存储个人信息
           uni.setStorageSync('userIfon', res)
         }
       },
      // 解析code
      getUrlCode() { // 截取url中的code方法
      	let url = location.search
      	let theRequest = new Object()
      	if (url.indexOf("?") != -1) {
      		let str = url.substr(1)
      		let strs = str.split("&")
      		for (let i = 0; i < strs.length; i++) {
      			theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
      		}
      	}
      	return theRequest
      },
       // 授权
       getAuthorization() {
         let pages = uni.getStorageSync('BackPath')
         if(!pages){
           pages = '/pages/home/index'
         }
        // return
         const vm = this
         let wxUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' // 授权微信地址
         let LocalAddress = encodeURIComponent("https://app.dev.jinlinjishu.com"+pages) // 回调地址
         let appid = 'wxd50cad2e58b2fcaa' // 公众号id
         let state = 1 // 页面传参
         let scope = "snsapi_userinfo" //弹框显示授权
         // 如果codeurl为真用户以授权，否则没授权
          if (vm.codeurl == null || vm.codeurl == "") {
            window.location.href =
            	wxUrl + appid + "&redirect_uri=" + LocalAddress + "&response_type=code&scope=" +
             scope + "&state=" + state +"&connect_redirect=1#wechat_redirect";	
          }
          else{
             vm.getToken()
          }
       },
       // 获取微信返回订单openId
       async getToken() {
         const vm = this
         const res = await $GET('/open/login/weixin-oauth',{
           code: this.codeurl,
           appName: 'shuidisw_mp'
         })
         if (res.access_token) {
           uni.setStorageSync('openId', res.openid)
           // 获取服务器返回的token
            uni.request({
                url: 'https://open.jinlinjishu.com/connect/token', //仅为示例，并非真实接口地址。
                header: {
                 'Content-Type': 'application/x-www-form-urlencoded',
               },
                data: {
                  appname: 'shuidisw_mp',
                  grant_type:"wechat_mp",
                  client_id:"open.admin",
                  openid: res.openid
                },
                method: 'POST',
                success: (data) => {
                  if (data.data.access_token) {
                    // 存储token
                    const token = 'Bearer ' + data.data.access_token
                    uni.setStorageSync('authorization', token)
                    this.getUsser()
                    // 判断是从哪里跳到登录页，就跳回哪里，否则就跳首页
                  }
                }
            })
         }
       },
     }
  }
</script>

<style>
</style>
